package ru.isys.yoomee.ui;

import java.util.ArrayList;

import ru.isys.yoomee.model.Chat;
import ru.isys.yoomee.model.Yoo;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ChatsTabActivity extends ActivityGroup
{
    ArrayList<Chat> mList;
    public static ChatsTabActivity group;
    private ArrayList<View> mHistory;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        group=this;
        mList = new ArrayList<Chat>();
        mHistory=new ArrayList<View>();
        mockChats();
        Intent chatAllIntent = new Intent(this, ChatsAllActivity.class);
        chatAllIntent.putExtra(ChatsAllActivity.CHATS_ALL_CHAT, mList);
        this.replaceContentView("ChatsAllActivity", chatAllIntent);
    }

    public void replaceContentView(String id, Intent newIntent)
    {
        View view = getLocalActivityManager().startActivity(id, newIntent).getDecorView();
        mHistory.add(view);
        this.setContentView(view);
    }

    public void back() {  
        if(mHistory.size() > 0) {  
            mHistory.remove(mHistory.size()-1);
            if(mHistory.size()<=0){
                finish();
            }else{
                setContentView(mHistory.get(mHistory.size()-1));
            }
        }else {  
            finish();  
        }  
    }
    
    private void mockChats()
    {
        Yoo owner = new Yoo("Mee");
        
        Chat chat = new Chat(owner, new Yoo("Anya"));        
        mList.add(chat);
        
        Chat chat2 = new Chat(owner, new Yoo("Ivan"));        
        mList.add(chat2);
        
        Chat chat3 = new Chat(owner, new Yoo("Den"));        
        mList.add(chat3);
        
        
        Chat chat4 = new Chat(owner, new Yoo("Anya"), new Yoo("Ivan"));        
        mList.add(chat4);
    }
    
    @Override  
    public void onBackPressed() {  
        ChatsTabActivity.group.back();
    }

}
